package com.bafa.services.results;

import com.bafa.jdbc.DBAccess;
import com.bafa.objects.Match;
import com.bafa.objects.Message;
import com.bafa.objects.team.Team;
import com.bafa.objects.team.TeamSeasonStats;
import com.bafa.services.RetrieveServices;

public class FunctionalResultsServices {

	static void addScores(Match match, int goalsA, int goalsB, boolean home) {
		match.setGoalsA(goalsA);
		match.setGoalsB(goalsB);
		match.setPlayed(true);
		if (home)
			match.setHomeAdded(true);
		else
			match.setAwayAdded(true);
	}

	static void resultDisputed(int matchId) {
		Match match = RetrieveServices.getMatch(matchId);

		Team teamA = RetrieveServices.getTeam("" + match.getTeamA());
		TeamSeasonStats seasonStats = (TeamSeasonStats) teamA.getStats()
				.getSeasonStats().get(match.getSeasonId());
		if (match.getGoalsA() > match.getGoalsB()) {
			teamA.getStats().setGamesWon(teamA.getStats().getGamesWon() - 1);
			seasonStats.setGamesWon(seasonStats.getGamesWon() - 1);
			if (match.getDivision() > 0) {
				seasonStats
						.setLeagueGamesWon(seasonStats.getLeagueGamesWon() - 1);
			}
		} else if (match.getGoalsA() < match.getGoalsB()) {
			teamA.getStats().setGamesLost(teamA.getStats().getGamesLost() - 1);
			seasonStats.setGamesLost(seasonStats.getGamesLost() - 1);
			if (match.getDivision() > 0) {
				seasonStats
						.setLeagueGamesLost(seasonStats.getLeagueGamesLost() - 1);
			}
		} else {
			teamA.getStats()
					.setGamesDrawn(teamA.getStats().getGamesDrawn() - 1);
			seasonStats.setGamesDrawn(seasonStats.getGamesDrawn() - 1);
			if (match.getDivision() > 0) {
				seasonStats.setLeagueGamesDrawn(seasonStats
						.getLeagueGamesDrawn() - 1);
			}
		}

		Team teamB = RetrieveServices.getTeam("" + match.getTeamB());
		TeamSeasonStats seasonStatsB = (TeamSeasonStats) teamB.getStats()
				.getSeasonStats().get(match.getSeasonId());
		if (match.getGoalsA() > match.getGoalsB()) {
			teamB.getStats().setGamesWon(teamB.getStats().getGamesWon() - 1);
			seasonStatsB.setGamesWon(seasonStatsB.getGamesWon() - 1);
			if (match.getDivision() > 0) {
				seasonStatsB
						.setLeagueGamesWon(seasonStatsB.getLeagueGamesWon() - 1);
			}
		} else if (match.getGoalsA() < match.getGoalsB()) {
			teamB.getStats().setGamesLost(teamB.getStats().getGamesLost() - 1);
			seasonStatsB.setGamesLost(seasonStatsB.getGamesLost() - 1);
			if (match.getDivision() > 0) {
				seasonStatsB.setLeagueGamesLost(seasonStatsB
						.getLeagueGamesLost() - 1);
			}
		} else {
			teamB.getStats()
					.setGamesDrawn(teamB.getStats().getGamesDrawn() - 1);
			seasonStatsB.setGamesDrawn(seasonStatsB.getGamesDrawn() - 1);
			if (match.getDivision() > 0) {
				seasonStatsB.setLeagueGamesDrawn(seasonStatsB
						.getLeagueGamesDrawn() - 1);
			}
		}
		match.setGoalsA(0);
		match.setGoalsB(0);
		match.setPlayed(false);
		match.setBlocked(true);
		try {
			DBAccess db = new DBAccess();
			db.updateTeam(teamA);
			db.updateTeam(teamB);
			db.updateMatch(match);
		} catch (Exception e) {

		}

	}

	public static Message confirmResult(int matchId) {
		DBAccess db = new DBAccess();

		Match match = RetrieveServices.getMatch(matchId);
		match.setConfirmed(true);
		match.setAwayAdded(true);
		match.setHomeAdded(true);

		Message message = new Message();
		try {
			db.updateMatch(match);
			message.addMessage("Match has been confirmed");
		} catch (Exception e) {
			message.addMessage("Has not confirmed");
		} finally {
			db.disconnect();
		}

		return message;
	}

}
